package com.tenc.runner;


import com.tenc.feign.InitService;
import com.tenc.utils.RedisUtil;
import com.tenc.pojo.ErrorJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author Alan
 * @since 2021-07-05
 */
@Component
@Slf4j
public class AccessTokenRunner implements ApplicationRunner {
    @Value("${wechat.company.corpid}")
    private String corpid;
    @Value("${wechat.company.corpsecret}")
    private String corpsecret;


    @Autowired
    InitService initService;

    /**
     * 获取微信Access_Token
     * @param args args
     * @throws Exception e
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {

        ErrorJson json = initService.getAccessToken(corpid,corpsecret);
        if(json.getErrcode()!=0){
            log.info("access_token获取失败\n错误原因：{}",json);
        }else{
            log.info("获取到AccessToken：{}",json);
            RedisUtil.set("acToken",json.getAccess_token());
            RedisUtil.expire("acToken",7200);
        }
        
        //TODO 开启定时任务

    }
}
